home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group00a.txt / 000125_icon-group-sender _Fri May 26 08:35:41 2000.msg < prev    next >
Internet Message Format  |  2001-01-03  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA02292
  4.     for icon-group-addresses; Fri, 26 May 2000 08:35:31 -0700 (MST)
  5. Message-Id: <200005261535.IAA02292@baskerville.CS.Arizona.EDU>
  6. From: Steve Wampler <swampler@noao.edu>
  7. X-Newsgroups: comp.lang.icon
  8. Subject: Re: CODE() and  @/2
  9. Date: Fri, 26 May 2000 07:54:20 -0700
  10. X-Trace: noao.edu 959352862 96002 140.252.38.6 (26 May 2000 14:54:22 GMT)
  11. X-Complaints-To: abuse@noao.edu
  12. X-Accept-Language: en
  13. To: icon-group@optima.CS.Arizona.EDU
  14. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  15. Status: RO
  16.  
  17. "F.G. van DORP" wrote:
  18. ...
  19. > Can  somebody please explain the infix @ operator  ?
  20. > The closest I can get  is something like:  @'s first argument gets  pushed
  21. > onto its second argument's  stack  before the latter is activated  (and
  22. > at  this  point my  mind goes  blank...  OK,  so  the  infix is just  a prefix
  23. > @ with &NULL as the first argument, which doesn't help me a lot either).
  24. > Its purpose is apparently  to have co-expressions communicate between
  25. > themselves (thus enabling  them to gang up against the programmer  ?)
  26.  
  27. Think of co-expressions a threads that don't execute simultaneously.
  28. Instead, co-expressions cooperate by explicitly passing control between
  29. them using the @ operator as a "transfer point".  Logically, the CPU resource
  30. is being passed among the co-expressions (remember that Icon's execution
  31. starts in a co-expression, referenced by &main).
  32.  
  33. Now, think of the @ operator from the point of view of the co-expression
  34. that is invoking it - the behavior of the @ is very much like that
  35. co-expression making a function call - execution of the co-expression
  36. (call it "A") is suspended until execution "returns" from the @ - perhaps by
  37. some
  38. other co-expression (call it "B") invoking the first via an @ [think about
  39. that for a moment, it's probably the confusing part!].  If B uses the
  40. infix @ - as in (say) "foo" @ A, then A will see its @ operator as "returning"
  41. the value "foo".
  42.  
  43. So, each co-expression sees @ as an operator that "invokes" another
  44. co-expression
  45. to produce a result.  The result can be produced in one of two ways:
  46.  
  47. (1) Since a co-expression is an expression, and since all expressions in Icon
  48.     produce results, there is an implicit transfer of the result of that
  49.     expression back to the invoking co-expression.  This is how most people
  50.     use co-expressions, as in:
  51.  
  52.        nextLabel := create "L" || seq()
  53.  
  54.     to produce a unique label whenever activated.  (Note that this can also
  55.     be done using a procedure, as in:
  56.  
  57.        procedure newLabel()
  58.           static suffix
  59.           initial suffix := 0
  60.           return "L" || (suffix +:= 1)
  61.        end
  62.  
  63.     but some people find the co-expression form simpler.  Also note that
  64.  
  65.        procedure newLabel2()
  66.           return "L" || seq()
  67.        end
  68.  
  69.     and
  70.  
  71.        procedure newLabel3()
  72.           suspend "L" || seq()
  73.        end
  74.  
  75.     do *not* perform the same function.)
  76.           
  77.  
  78. (2) A co-expression may explicitly return a value to a waiting co-expression
  79.     using the binary @ operator.  The explicit transfer of control used here
  80.     more closely matches the role of coroutines found in some other languages,
  81.     and is likely to be used only in situations where coroutines would be used
  82.     in those other languages.  However, useful situations for applying
  83. coroutines
  84.     are few and far between and almost always very complex situations.  It is
  85.     difficult to find simple examples where using coroutines is cleaner than
  86.     alternatives.
  87.  
  88.   
  89.  
  90. --
  91. Steve Wampler-  SOLIS Project, National Solar Observatory
  92. swampler@noao.edu
  93.